package com.lanlian.cayman.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqAddFollowUser;
import com.lanlian.commonlib.data.ContractBean;
import com.lanlian.commonlib.event.EventCode;
import com.lanlian.commonlib.event.EventMessage;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.commonlib.util.EventBusUtils;
import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.cayman.view.ChooseBeiShuDialog;
import com.lanlian.cayman.view.ChooseContractDialog;
import com.lanlian.uikit.CustomDialog;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/22 13:48
 *     desc   : 交易跟单
 *     version: 1.0
 * </pre>
 */
public class TradeDocumentaryActivity extends BaseActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

    private EditText mEtDocumentaryId;
    private TextView mTvAllContract;
    private RadioGroup mRgAmount;
    private TextView mTvTimes;
    private EditText mEtLots;
    private RadioGroup mRgDirection;
    private RadioGroup mRgPrice;
    private EditText mEtOverPrice;
    private TextView mTvDocumentary;

    /**
     * 1：倍数，2：手数
     */
    private int mTimes = 1;
    /**
     * 1：正向，2：反向
     */
    private int mDirection = 1;
    /**
     * 1：市价跟单，2：超价跟单
     */
    private int mPrice = 1;

    private ChooseContractDialog mChooseContractDialog;
    private ChooseBeiShuDialog mChooseBeiShuDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_documentary, true);
        initView();
        initData();
        setListeners();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("我要跟单");
        setRightImage(R.mipmap.documentary_icon_problem);
        mEtDocumentaryId = findViewById(R.id.et_documentary_id);
        mTvAllContract = findViewById(R.id.tv_all_contract);
        mRgAmount = findViewById(R.id.rg_amount);
        mTvTimes = findViewById(R.id.tv_times);
        mEtLots = findViewById(R.id.et_lots);
        mRgDirection = findViewById(R.id.rg_direction);
        mRgPrice = findViewById(R.id.rg_price);
        mEtOverPrice = findViewById(R.id.et_over_price);
        mTvDocumentary = findViewById(R.id.tv_documentary);
        mTvAllContract.setTag("*");
    }

    @Override
    protected void initData() {
        super.initData();
        long no = getIntent().getLongExtra("no", 0);
        if (no != 0) {
            mEtDocumentaryId.setText(String.valueOf(no));
        }
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvTimes.setOnClickListener(this);
        mTvAllContract.setOnClickListener(this);
        mRgAmount.setOnCheckedChangeListener(this);
        mRgPrice.setOnCheckedChangeListener(this);
        mRgDirection.setOnCheckedChangeListener(this);
        mImgAction.setOnClickListener(this);
        mTvDocumentary.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.img_action:
                new CustomDialog(this, new CustomDialog.OnClickListener() {
                    @Override
                    public void onLeftClick() {
                    }

                    @Override
                    public void onRightClick() {
                    }
                }).setOneBtn(true).setTitle("跟单提示").setContent("1.跟单时，请确保资金充足\n" +
                        "2.如取消跟单，请手动撤销跟单\n" +
                        "3.跟单开始时不可有跟单合约的持仓和委托单，请平仓或撤销委托后进行跟单操作\n" +
                        "4.跟单开始后，如果手动对跟单合约进行买、卖、平仓、全部平仓、止盈止损、市价反手、撤单的操作，则跟单会被系统取消\n" +
                        "5.只可跟一个账户的操作，不可同时跟单多个账户\n" +
                        "6.退出交易系统，未撤销的跟单仍会继续执行")
                        .setRight("我知道了").setGravity(Gravity.LEFT).show();
                break;
            case R.id.tv_all_contract:
                if (mChooseContractDialog == null) {
                    mChooseContractDialog = new ChooseContractDialog(this, new ChooseContractDialog.OnChooseContractListener() {
                        @Override
                        public void onChooseContract(ContractBean cb) {
                            if ("*".equals(cb.contractNo)) {
                                mTvAllContract.setText("全部");
                            } else {
                                mTvAllContract.setText(String.format("%s (%S)", cb.contractName, cb.contractNo));
                            }
                            mTvAllContract.setTag(cb.contractNo);
                        }
                    });
                }
                mChooseContractDialog.showDialog();
                break;
            case R.id.tv_times:
                if (mChooseBeiShuDialog == null) {
                    mChooseBeiShuDialog = new ChooseBeiShuDialog(this, new ChooseBeiShuDialog.OnChooseContractListener() {
                        @Override
                        public void onChooseContract(Object obj) {
                            mTvTimes.setText(obj.toString());
                        }
                    });
                }
                mChooseBeiShuDialog.showDialog();
                break;
            case R.id.tv_documentary:
                if (mDBManager.isShowDocumentaryTips()) {
                    new CustomDialog(this, new CustomDialog.OnClickListener() {
                        @Override
                        public void onLeftClick() {
                            mDBManager.setShowDocumentaryTips();
                            if (check()) {
                                addData();
                            }
                        }

                        @Override
                        public void onRightClick() {
                            if (check()) {
                                addData();
                            }
                        }
                    }).setTitle("跟单提示").setContent("1.跟单时，请确保资金充足\n" +
                            "2.如取消跟单，请手动撤销跟单\n" +
                            "3.跟单开始时不可有跟单合约的持仓和委托单，请平仓或撤销委托后进行跟单操作\n" +
                            "4.跟单开始后，如果手动对跟单合约进行买、卖、平仓、全部平仓、止盈止损、市价反手、撤单的操作，则跟单会被系统取消\n" +
                            "5.只可跟一个账户的操作，不可同时跟单多个账户\n" +
                            "6.退出交易系统，未撤销的跟单仍会继续执行")
                            .setRight("我知道了").setLeft("不再提示").setGravity(Gravity.LEFT).show();
                } else {
                    if (check()) {
                        addData();
                    }
                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int id = group.getId();
        switch (id) {
            case R.id.rg_amount:
                switch (checkedId) {
                    case R.id.rb_times:
                        mTimes = 1;
                        mTvTimes.setText("1");
                        mEtLots.setText("");
                        mTvTimes.setTextColor(getResources().getColor(R.color.color_text_style1));
                        mEtLots.setEnabled(false);
                        mTvTimes.setEnabled(true);
                        break;
                    case R.id.rb_lots:
                        mTimes = 2;
                        mTvTimes.setText("请选择倍数");
                        mEtLots.setText("1");
                        mTvTimes.setTextColor(getResources().getColor(R.color.color_text_style11));
                        mEtLots.setEnabled(true);
                        mTvTimes.setEnabled(false);
                        break;
                    default:
                        break;
                }
                break;
            case R.id.rg_direction:
                switch (checkedId) {
                    case R.id.rb_forward:
                        mDirection = 1;
                        break;
                    case R.id.rb_reverse:
                        mDirection = 2;
                        break;
                    default:
                        break;
                }
                break;
            case R.id.rg_price:
                switch (checkedId) {
                    case R.id.rb_market_price:
                        mPrice = 1;
                        mEtOverPrice.setText("");
                        mEtOverPrice.setEnabled(false);
                        break;
                    case R.id.rb_over_price:
                        mPrice = 2;
                        mEtOverPrice.setEnabled(true);
                        break;
                    default:
                        break;
                }
                break;

            default:
                break;
        }
    }

    private ReqAddFollowUser getParam() {
        ReqAddFollowUser rm = new ReqAddFollowUser();
        rm.setUserNo(mEtDocumentaryId.getText().toString());
        rm.setFutureCode(mTvAllContract.getTag().toString());
        rm.setHandType(mTimes + "");
        rm.setOperationType("1");
        if (mTimes == 1) {
            rm.setHandNum(mTvTimes.getText().toString());
        } else {
            rm.setHandNum(mEtLots.getText().toString());
        }
        rm.setOrderDirection(mDirection + "");
        rm.setFollowType(mPrice + "");
        if (!TextUtils.isEmpty(mEtOverPrice.getText().toString())) {
            rm.setFollowNum(mEtOverPrice.getText().toString());
        }
        return rm;
    }

    private boolean check() {
        ReqAddFollowUser req = getParam();
        if (TextUtils.isEmpty(req.getUserNo())) {
            showToast("请输入用户账号或跟单编号");
            return false;
        }
        if (req.getHandNum() == null || "0".equals(req.getHandNum())
                || "".equals(req.getHandNum()) || "请选择倍数".equals(req.getHandNum())) {
            showToast("请输入跟单数量");
            return false;
        }
        if ("2".equals(req.getFollowType()) && "0".equals(req.getFollowNum())) {
            showToast("超价点数必须为大于等于1的整数");
            return false;
        }
        return true;
    }

    private void addData() {
        showLoadingDialog();
        ReqAddFollowUser rm = getParam();
        VolleyRequestUtils.getInstance().sendRequest(this.toString(), rm, new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                dismissLoadingDialog();
                showToast("跟单成功");
                EventBusUtils.post(new EventMessage(EventCode.EVENT_DOCUMENTARY_REFRESH));
                finish();
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                showToast(baseResponse.getMsg());
                dismissLoadingDialog();
            }
        });
    }

}
